/*
 * @Date: 2021-09-05 20:28:39
 * @Author: Acckno1
 * @LastEditTime: 2021-09-07 20:32:58
 * @Description: 
 */
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 1e4 + 10;

int n, m;
int p[N];
unordered_set<int> st;
unordered_set<int> bd;

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

inline void solution() {
    for (int i = 0; i < N; i ++ ) p[i] = i;

    cin >> n;
    int count = 0;
    for (int i = 0; i < n; i ++ ) {
        int cnt;
        cin >> cnt;
        int k[N];
        for (int j = 0; j < cnt; j ++ ) {
            cin >> k[j];
            bd.insert(k[j]);
        }
        for (int j = 0; j < cnt; j ++ ) {
            int a = k[0], b = k[j];
            int pa = find(a), pb = find(b);
            if (pa != pb) {
                p[pb] = pa;
                count ++;
            }
        }
    }
    for (auto & b : bd) st.insert(p[b]);
    // 树的数量 = 鸟的数量 - 合并次数
    cout << bd.size() -  count << " " << bd.size() << endl;
    cin >> m;
    while (m -- ) {
        int a, b;
        cin >> a >> b;
        if(find(a) != find(b)) cout << "No" << endl;
        else cout << "Yes" << endl;
    }
}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}